<?php

namespace app\store\controller;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
use think\Db;
use think\Log;
use Predis\Client;
/**
 *  dingyue
 */
class Sub{
    //订阅
    public function index(){
        $server = [
            'host' => '127.0.0.1',
            'port' => 6379,
        ];
        $redis = new Client($server);
        $pubsub = $redis->pubSubLoop();
        $pubsub->subscribe('order', 'notifications');
        foreach ($pubsub as $message) {
            switch ($message->kind) {
                case 'subscribe':
                    echo "Subscribed to {$message->channel}", PHP_EOL;
                    break;
                case 'message':
                    if ($message->channel == 'control_channel') {
                        if ($message->payload == 'quit_loop') {
                            echo 'Aborting pubsub loop...', PHP_EOL;
                            $pubsub->unsubscribe();
                        } else {
                            echo "Received an unrecognized command: {$message->payload}.", PHP_EOL;
                        }
                    } else {
                        echo "Received the following message from {$message->channel}:",
                        PHP_EOL, " {$message->payload}", PHP_EOL, PHP_EOL;
                    }
                    break;
            }
        }
    }
}

